/* Restatement.do (STATA)
	Construct restatement matrices.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Restatement, replace;

/* Define local and global variables */

local directory = "../../Data/Global Capital Allocation";

do global;


/* Step 1: Prepare countries */

u country Yeuro using Countries, clear;

/* Add currency reserves */

input;
_CR .;
end;

/* Construct variables */

gen Investor = "EMU" if !missing(Yeuro);
replace Investor = "World" if missing(Yeuro);

drop Yeuro;

/* Save data */

tempfile Countries;
save `Countries';


/* Step 2: Construct restatement matrices for issuances */

/* Load restatement matrices */

u Methodology Year Asset_Class Destination Destination_Restated Value
	if Methodology==3			/* Issuance */
	& inrange(Year,$year_min,$year_max)
	& inlist(Asset_Class,1,3)	/* 1 = All Bonds, 3 = Common Equity */
	using "`directory'/Restatement_Matrices", clear;

drop Methodology;

/* Rename variables */

rename Year year;
rename Destination Counterpart;
rename Destination_Restated counterpart;
/*
/* Merge country code */

merge m:1 Counterpart using Countries,
	keepusing(Itax)
	nogen keep(match);

/* Keep tax havens only */

keep if Itax;

drop Itax;
*/
/* Correct rounding error */

egen TValue = total(Value), missing by(year Counterpart Asset_Class);

replace Value = Value/TValue;

drop TValue;

/* Expand to ST and LT debt */

expand 3 if Asset_Class==1;

bysort year Counterpart counterpart Asset_Class: gen byte type = Asset_Class+(Asset_Class==1)*(_n-2);

drop Asset_Class;

/* Extrapolate backward */

egen int Myear = min(year);

gen byte obs = year-$year_min+1 if year==Myear;

expand obs;

bysort year Counterpart counterpart type: replace year = year-_n+1;

drop Myear obs;

/* Extrapolate forward */

egen int Myear = max(year);

gen byte obs = $year_max-year+1 if year==Myear;

expand obs;

bysort year Counterpart counterpart type: replace year = year+_n-1;

drop Myear obs;

/* Label variables */

order year Counterpart counterpart type;

label var year			"Year";
label var Counterpart	"Issuer country by residency";
label var counterpart	"Issuer country by nationality";
label var type			"Asset type";

label var Value			"Restatement share from residency to nationality";

label define type_label
	0 "All debt"
	1 "Short-term debt"
	2 "Long-term debt"
	3 "Equity", replace;

label val type type_label;

/* Save data */

sort year Counterpart counterpart type;

save Restatement_issuance, replace;


/* Step 3: Construct share in common equity */

/* Load restated bilateral portfolios */

u Methodology Year Investor Asset_Class Issuer Position_Residency Estimated_Common_Equity
	if inrange(Year,$year_min,$year_max)
	& Asset_Class==4	/* Common Equity and Fund Shares */
	& Position_Residency>0
	& !missing(Estimated_Common_Equity)
	using "`directory'/Restated_Bilateral_External_Portfolios", clear;

drop Asset_Class;

/* Keep best methodology */

egen byte best = min(Methodology), by(Year Investor);

keep if Methodology==best;

drop Methodology best;

/* Rename variables */

rename Year year;
rename Issuer Counterpart;

/* Construct variables */

gen byte type = 3;

gen Scommon = min(Estimated_Common_Equity/Position_Residency,1);

drop Estimated_Common_Equity Position_Residency;

/* Merge country code */

joinby Investor using `Countries', unmatched(master);

replace country = Investor if _merge==1;

drop Investor _merge;

/* Extrapolate backward */

egen int Myear = min(year), by(country);

gen byte obs = year-$year_min+1 if year==Myear;

expand obs;

bysort year country Counterpart type: replace year = year-_n+1;

drop Myear obs;

/* Extrapolate forward */

egen int Myear = max(year), by(country);

gen byte obs = $year_max-year+1 if year==Myear;

expand obs;

bysort year country Counterpart type: replace year = year+_n-1;

drop Myear obs;

/* Label variables */

order year country Counterpart type;

label var year			"Year";
label var country		"Investor country";
label var Counterpart	"Issuer country by residency";
label var type			"Asset type";

label var Scommon		"Share in common equity";

label define type_label
	1 "Short-term debt"
	2 "Long-term debt"
	3 "Equity";

label val type type_label;

/* Save data */

sort year country Counterpart type;

save Restatement_equity, replace;


/* Step 4: Construct asset class shares for USA */

/* Load restated bilateral portfolios */

u Methodology Year Investor Asset_Class Issuer Position_Residency
	if inrange(Year,$year_min,$year_max)
	& Investor=="USA"
	& inlist(Asset_Class,2,5,6)	/* 2 = Asset-Backed Securities, 5 = Corporate Bonds, 6 = Government Bonds */
	using "`directory'/Restated_Bilateral_External_Portfolios", clear;

/* Keep best methodology */

egen byte best = min(Methodology), by(Year Asset_Class);

keep if Methodology==best;

drop Methodology best;

/* Rename variables */

rename Year year;
rename Issuer Counterpart;

/* Fix variables */

replace Asset_Class = Asset_Class-1 if Asset_Class>=5;

/* Construct variables */

egen TPosition_Residency = total(Position_Residency), missing by(year Counterpart);

gen weight = Position_Residency/TPosition_Residency;	/* Value weighted if available */
replace weight = 1/3 if TPosition_Residency==0;			/* Equal weighted otherwise */

drop Position_Residency TPosition_Residency;

/* Save data */

tempfile USA;
save `USA';


/* Step 5: Construct restatement matrices for bilteral positions */

/* Load restatement matrices */

u Methodology Year Investor Asset_Class Destination Destination_Restated Value
	if inrange(Year,$year_min,$year_max)
	& (Investor=="USA" | Asset_Class!=4)	/* 4 = Corporate Bonds */
	using "`directory'/Restatement_Matrices", clear;

/* Keep best methodology */

egen byte best = min(Methodology), by(Year Investor Asset_Class);

keep if Methodology==best;

drop Methodology best;

/* Rename variables */

rename Year year;
rename Destination Counterpart;
rename Destination_Restated counterpart;
/*
/* Merge country code */

merge m:1 Counterpart using Countries,
	keepusing(Itax)
	nogen keep(match);

/* Keep tax havens only */

keep if Itax;

drop Itax;
*/
/* Merge asset class shares for USA */

merge m:1 year Investor Counterpart Asset_Class using `USA';

/* Assume nationality is same as residency if unknown */

replace counterpart = Counterpart if _merge==2;
replace Value = 1 if _merge==2;

drop _merge;

/* Construct "All Bonds" for USA */

egen TValue = total(weight*Value) if Investor=="USA" & inlist(Asset_Class,2,4,5), missing by(year Investor Counterpart counterpart);

replace Value = TValue if Investor=="USA" & inlist(Asset_Class,2,4,5) & !missing(TValue);
replace Asset_Class = 1 if Investor=="USA" & inlist(Asset_Class,2,4,5);

bysort year Investor Counterpart counterpart Asset_Class: keep if _n==1;

drop weight TValue;

/* Correct rounding error */

egen TValue = total(Value), missing by(year Investor Counterpart Asset_Class);

replace Value = Value/TValue;

drop TValue;

/* Merge country code */

joinby Investor using `Countries', unmatched(master);

replace country = Investor if _merge==1;

/* Drop if country-specific restatement available */

sort year country _merge;
by year country: drop if _merge[1]==1 & _merge==3;

drop Investor _merge;

/* Expand to ST and LT debt */

expand 2 if Asset_Class==1, gen(duplicate);

gen byte type = Asset_Class+duplicate;

drop Asset_Class duplicate;

/* Extrapolate backward */

egen int Myear = min(year), by(country);

gen byte obs = year-$year_min+1 if year==Myear;

expand obs;

bysort year country Counterpart counterpart type: replace year = year-_n+1;

drop Myear obs;

/* Extrapolate forward */

egen int Myear = max(year), by(country);

gen byte obs = $year_max-year+1 if year==Myear;

expand obs;

bysort year country Counterpart counterpart type: replace year = year+_n-1;

drop Myear obs;

/* Label variables */

order year country Counterpart counterpart type;

label var year			"Year";
label var country		"Investor country";
label var Counterpart	"Issuer country by residency";
label var counterpart	"Issuer country by nationality";
label var type			"Asset type";

label var Value			"Restatement share from residency to nationality";

label define type_label
	1 "Short-term debt"
	2 "Long-term debt"
	3 "Equity";

label val type type_label;

/* Save data */

sort year country Counterpart counterpart type;

save Restatement_bilateral, replace;

log close;
